clear
capture log close
set more off
set mem 500m




****

use data_pt_pop_1891, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc
merge loc using data_town_cotton_shr_1851
tab _merge
keep if _merge==3
drop _merge


** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1

** Drop cotton towns
drop if cotton10==1

*Identify textile cities
replace shr_wool=0 if shr_wool==.
gen shr_tex =  shr_wool + shr_other_tex


** Drop liverpool -- which was affected by these events in a unique way
** because it was the main cotton textile port city
drop if loc=="liverpool"


*** Merge in geographic proximity
sort loc
merge loc using data_dist_to_manchester
tab _merge
keep if _merge==3
drop _merge


*** Growth rates
sort loc year
gen growth=log_pop-log_pop[_n-1] if loc==loc[_n-1]

*** Focus on data from 1851-1871 
drop if year==1841
drop if year>1871

keep loc year growth dist shr_tex

reshape wide growth, i(loc) j(year)

*** change in growth rates between 1851-1861 and 1861-1871
sort loc
gen chg=growth1871-growth1861


*** distance measure
gen dd=exp(-dist/1000)

** Standardize variables
egen dd_std=std(dd)
egen shr_tex_std=std(shr_tex)

** Interaction variable
gen interact=dd_std*shr_tex_std


***** Merge in lat and lon data
sort loc
merge loc using data_city_lat_lon_data
tab _merge
keep if _merge==3
drop _merge


** Label variables
label variable dd_std "Distance to Manchester (std)"
label variable shr_tex_std "Non-cotton textile employment share (std)"
label variable interact "Distance x Non-cotton textile employment share"

*** Regressions -- textile used as similarity measure

reg chg dd_std, robust
est store DD

reg chg shr_tex_std, robust
est store TEX

reg chg dd_std shr_tex_std, robust
est store BOTH

reg chg dd_std shr_tex_std interact, robust
est store INTERACT

outreg2 [DD TEX BOTH INTERACT] using results_table_6, tex replace see label  drop(_I*)


** Prepare for spatial regressions
gen year=1871
encode loc, gen(loc_code)
xtset loc_code year
gen constant=1

ols_spatial_HAC chg dd_std constant, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store DD2

ols_spatial_HAC chg shr_tex_std constant, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store TEX2

ols_spatial_HAC chg dd_std shr_tex_std constant, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store BOTH2

ols_spatial_HAC chg dd_std shr_tex_std interact constant, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store INTERACT2

outreg2 [DD2 TEX2 BOTH2 INTERACT2] using results_table_6_spatial_SEs, tex replace see label  drop(_I*)

